package tool.paoshang;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.google.common.collect.Lists;

import tool.dataType.WindowsClient;
import tool.exception.Zy;
import tool.image.ImageTool;
import tool.keep.Keeper;
import tool.keep.Keeper.product;
import tool.mouse.MouseTool;
import tool.windows.W32API.HWND;
import tool.windows.WindowsTool;
import tool.windows.struct.LPRECTlpRect;

public class PaoShang {
	
	public static Point point= new Point();
	public static Point p= new Point();
	public static HWND hwnd;
	public static Point gotobpcsr = new Point(558,200);
	public static Point daodabpcsr = new Point(510,224);
	public static Point djbpcsrwz = new Point(408,256);
	public static Point djsqsr = new Point(368,401);
	public static Point djgwrw = new Point(117,352);
	public static Point gotocahs = new Point(439,253);
	public static Point djcahs = new Point(443,243);
	public static Point gotocasr = new Point(353,307);
	public static Point djcasr = new Point(246,308);
	public static Point djwyjy = new Point(108,333);
	public static Point gotodtjw = new Point(95,400);
	public static Point gotosddtjw = new Point(20,295);
	public static Point gotojwcsjw = new Point(87,326);
	public static Point djjwcsjwtd = new Point(187,238);
	public static Point gotocsc = new Point(390,161);
	public static Point gotosdcsc = new Point(353,64);
	public static Point gotocshs = new Point(313,411);
	public static Point djcshs = new Point(200,248);
	public static Point gotocssr = new Point(378,374);
	public static Point djcssr = new Point(275,197);
	public static Point gotocscsjw = new Point(379,428);
	public static Point gotosdcscsjw = new Point(467,490);
	public static Point gotocsjwbjlz = new Point(240,323);
	public static Point djcsjwbjlzyz = new Point(261,275);
	public static Point gotobjlzca = new Point(195, 239);
	public static Point djbjlzcayz = new Point(215,275);
	
	
	public static Point cayzlb1 = new Point(220,179);
	public static Point cayzlb2 = new Point(400,179);
	
	
	public static Point djwyq = new Point(113,333);
	public static Point djgbsrdhk = new Point(315,437);
	public static Point djcszdslwz = new Point(383,411);
	public static Point djcsqd = new Point(386,460);
	public static Point djgmzdslwz = new Point(93,407);
	public static Point djgmqd = new Point(99,456);
	
	public static Point djgbpackage = new Point(99,456);
	
	public static Point sp1zb = new Point(80,150);
	
	public static Point gotobjcsjw = new Point(417,425);
	public static Point gotocayz = new Point(329,383);
	
	public static Point packageStartPoint = new Point(339,211);
	public static Point jynpcStartPoint = new Point(80,158);
	public static Point jymyStartPoint = new Point(372,158);

	public static Point jynpcypjeStartPoint = new Point(171,452);
	public static Point jynpczjeStartPoint = new Point(171,428);
	public static Point jynpcslStartPoint = new Point(171,404);
	public static Point jynpcdjStartPoint = new Point(171,380);

	public static Point jymyypjeStartPoint = new Point(463,452);
	public static Point jymyzjeStartPoint = new Point(463,428);
	public static Point jymyslStartPoint = new Point(463,404);
	public static Point jymydjStartPoint = new Point(463,380);
	public static JTextArea log = null; 
	public static JScrollPane scrollPane_1 = new JScrollPane();
	public static JScrollBar jsb = scrollPane_1.getVerticalScrollBar();
	public static int maxMoney;
	
	public static long time;
	
	public static boolean jobdone = false;
	
	public static Point mouse = new Point();
	public static boolean next = false;

	public static List<ProductItem> jyNpcProducts;
	public static List<ProductItem> jyMyProducts;
	public static List<ProductItem> myPackageProducts;
	private static boolean waitflag = false;
	private static Thread thread=null;

	public static boolean sqsr = false;
	public static String jcbstr;
	public static String pslxstr;
	
	
	public static void main(String[] args){

		JFrame jf = new JFrame("跑商测试");
		jf.setLayout(new BorderLayout());
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new GridLayout(4,1));
		jf.add(topPanel,BorderLayout.NORTH);
		JTextField client = new JTextField("点击选择绑定终端");
		client.setCaretColor(Color.green);
		client.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
			}
			public void mousePressed(MouseEvent e) {
			}
			public void mouseExited(MouseEvent e) {
			}
			public void mouseEntered(MouseEvent e) {
			}
			public void mouseClicked(MouseEvent e) {
				List<WindowsClient> clients = WindowsTool.mhClients();
				Object[] obj = new Object[clients.size()];
				for(int i =0 ; i < clients.size();i++){
					obj[i] = clients.get(i).getShowName();
				}
				String option = (String) JOptionPane.showInputDialog(null, "请选择绑定的客户端\n","选择绑定",JOptionPane.PLAIN_MESSAGE,null,obj,"");
				if(option == null || option == ""){
					return;
				}
				for(WindowsClient wclient : clients){
					if(wclient.getShowName().equals(option)){
						hwnd = wclient.getHwnd();
						client.setText(wclient.getShowName());
						LPRECTlpRect lrect = ImageTool.getClientRect(hwnd);
						point.x = lrect.x;
						point.y = lrect.y;
						break;
					}
				}
				client.setText(option);
			}
		});
		topPanel.add(client);
		client.setEditable(false);
		client.setBounds(0, 40, 110, 20);
		JPanel jp2 = new JPanel();
		jp2.setLayout(new GridLayout(1, 2));
		JLabel jl2 = new JLabel("交票金额");
		JTextField maxmoney = new JTextField();
		jp2.add(jl2);
		jp2.add(maxmoney);
		JButton jb2 = new JButton("开始跑商");
		log = new JTextArea();
		log.setEditable(false);
        scrollPane_1.setViewportView(log);
		jb2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(hwnd==null) {
					JOptionPane.showMessageDialog(null, "请先绑定窗口");
					return;
				}
				maxMoney = Integer.parseInt(maxmoney.getText());
				maxmoney.setEditable(false);
				jb2.setEnabled(false);
				thread = new Thread(()->{

					while(true) {
						WindowsTool.getForce(hwnd);
						Luxian.sleep(500);
						Luxian.bangpai(hwnd);
						while(true) {
							if(time<System.currentTimeMillis()) {
								while(true) {
									try {
										Luxian.eatSYX(hwnd);
										break;
									} catch (Zy z) {
										JOptionPane.showMessageDialog(null, "购买摄妖香后点击确定");
										WindowsTool.getForce(hwnd);
										Luxian.sleep(500);
									}
								}

							}
							try {
								Luxian.cacstl2();
							}catch(Zy e1) {
								if(e1.getCode()==-99) {
									break;
								}
							}
						}
					}
				}); 
				thread.start();
			}
		});
		JButton jb3 = new JButton("强制退出");
		jb3.addActionListener((e) ->{
			System.exit(0);
		});

		topPanel.add(jp2);
		topPanel.add(jb2);
		topPanel.add(jb3);
		jf.add(scrollPane_1,BorderLayout.CENTER);
		jf.setSize(250, 450);
		jf.setVisible(true);
		jf.addWindowListener(new WindowListener() {
			@Override
			public void windowOpened(WindowEvent e) {
			}
			@Override
			public void windowIconified(WindowEvent e) {
			}
			@Override
			public void windowDeiconified(WindowEvent e) {
			}
			@Override
			public void windowDeactivated(WindowEvent e) {
			}
			@Override
			public void windowClosing(WindowEvent e) {
				File f = new File(System.currentTimeMillis() + ".log");
				FileOutputStream fos;
				try {
					fos = new FileOutputStream(f);
					fos.write(log.getText().getBytes());
					fos.flush();
					fos.close();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
					print(e1.getMessage());
				} catch (IOException e1) {
					e1.printStackTrace();
					print(e1.getMessage());
				}
				System.exit(0);
			}
			@Override
			public void windowClosed(WindowEvent e) {
			}
			@Override
			public void windowActivated(WindowEvent e) {
			}
		});
	}

	public static void print(String string) {
		System.out.println(string);
		log.setText(log.getText()+"\r\n"+string);
		jsb.setValue(jsb.getMaximum());
	}

	protected static void colseRight(HWND hwnd,Point cack) {
		int time = 0;
		Luxian.sleep(500);
		BufferedImage bi = ImageTool.getScreenShoot(hwnd);
		boolean right = false;
		for(int i=-1;i<3;i++) {
			for(int j=-1;j<3;j++) {
				if(bi.getRGB(cack.x+j, cack.y+i)!=Keeper.mapUnrRightColor) {
					MouseTool.MoveRelative(hwnd, cack,3);
					MouseTool.leftClick();
					return;
				}
			}
		}
	}

	public static boolean validatejobdone(HWND hwnd,Point p, int maxMoney) {
		int money = getMoney(hwnd,p);
		print("当前银票金额：" + money);
		if(money>maxMoney) {
			return true;
		}else {
			return false;
		}
	}
	
	public static int getMoney(HWND hwnd,Point p) {
		BufferedImage bi = ImageTool.getScreenShoot(hwnd);
		return getMoney(bi,p);
	}

	public static int getMoney(BufferedImage bi,Point p) {
		int money = 0;
		for(int i=0;i<6;i++) {
			Integer no = getNumber(bi,p.x + i*7,p.y);
			if(no!=null) {
				money*=10;
				money+=no;
			}else {
				break;
			}
		}
		return money;
	}

	
	public static int getOwnMoney(HWND hwnd,Point p) {
		BufferedImage bi = ImageTool.getScreenShoot(hwnd);
		return getMoney(bi,p);
	}

	public static int getOwnMoney(BufferedImage bi,Point p) {
		int money = 0;
		for(int i=0;i<6;i++) {
			Integer no = getNumber(bi,p.x + i*7,p.y);
			if(no!=null) {
				money*=10;
				money+=no;
			}else {
				break;
			}
		}
		return money;
	}
	private static Integer getNumber(BufferedImage bi, int x, int y) {
		
		int[] tezheng = new int[10];
		int count = 0;
		for(int i=0;i<10;i++) {
			count = 0;
			for(int j=0;j<6;j++) {
				int color = bi.getRGB(x+j, y+i);
				if(color==Keeper.colorblack || color==Keeper.colorblue || color == Keeper.colorgreen) count++;
			}
			tezheng[i] = count;
		}
		for(Keeper.jyNumberPoints number : Keeper.jyNumberPoints.values()) {
			boolean continueflag = false;
			for(int i=0;i<10;i++) {
				if(tezheng[i]!=number.getJyNumber()[i]) {
					continueflag = true;
				}
			}
			if(continueflag) {
				continue;
			}
			return number.getNumber();
		}
		
		return null;
	}

	public static List<ProductItem> getAllMyProducts(HWND hwnd){
		BufferedImage gamebi = ImageTool.getScreenShoot(hwnd);
		return getAllMyProducts(gamebi);
	}

	public static List<ProductItem> getAllMyProducts(BufferedImage gamebi){
		List<ProductItem> myProducts = Lists.newArrayList();
		for(int y = 0;y<5;y++) {
			for(int x = 0;x<5;x++) {
				product productcolor = getProduct(372+51*x,158+51*y,gamebi);
				if(productcolor!=null) {
					ProductItem productItem = new ProductItem();
					productItem.setItem(productcolor);
					productItem.setX(x);
					productItem.setY(y);
					myProducts.add(productItem);
				}
			}
		}
		return myProducts;
	}

	public static List<ProductItem> getAllNpcProducts(HWND hwnd){
		BufferedImage gamebi = ImageTool.getScreenShoot(hwnd);
		return getAllNpcProducts(gamebi);
	}

	public static List<ProductItem> getAllNpcProducts(BufferedImage gamebi){
		List<ProductItem> myProducts = Lists.newArrayList();
			for(int x = 0;x<5;x++) {
				product productcolor = getProduct(80+51*x,158,gamebi);
				if(productcolor!=null) {
					ProductItem productItem = new ProductItem();
					productItem.setItem(productcolor);
					productItem.setX(x);
					productItem.setY(0);
					myProducts.add(productItem);
				}
		}
		return myProducts;
	}

	public static List<ProductItem> getPackageProducts(HWND hwnd){
		BufferedImage gamebi = ImageTool.getScreenShoot(hwnd);
		return getPackageProducts(gamebi);
	}

	public static List<ProductItem> getPackageProducts(BufferedImage gamebi){
		List<ProductItem> myProducts = Lists.newArrayList();
		for(int y=0;y<5;y++) {
			for(int x = 0;x<5;x++) {
				product productcolor = getProduct(packageStartPoint.x+51*x,packageStartPoint.y+y*51,gamebi);
				if(productcolor!=null) {
					ProductItem productItem = new ProductItem();
					productItem.setItem(productcolor);
					productItem.setX(x);
					productItem.setY(y);
					myProducts.add(productItem);
				}
			}
		}
		return myProducts;
	}

	private static product getProduct(int x, int y, BufferedImage gamebi) {

		if(gamebi.getRGB(x,y) == Keeper.noProductColor)
			return null;
		for(product productcolor:product.values()) {

			if(gamebi.getRGB(x-1,y-1)!=productcolor.getColor()[0][0]) {
				continue;
			}
			if(gamebi.getRGB(x,y-1)!=productcolor.getColor()[0][1]) {
				continue;
			}
			if(gamebi.getRGB(x+1,y-1)!=productcolor.getColor()[0][2]) {
				continue;
			}
			if(gamebi.getRGB(x-1,y)!=productcolor.getColor()[1][0]) {
				continue;
			}
			if(gamebi.getRGB(x,y)!=productcolor.getColor()[1][1]) {
				continue;
			}
			if(gamebi.getRGB(x+1,y)!=productcolor.getColor()[1][2]) {
				continue;
			}
			if(gamebi.getRGB(x-1,y+1)!=productcolor.getColor()[2][0]) {
				continue;
			}
			if(gamebi.getRGB(x,y+1)!=productcolor.getColor()[2][1]) {
				continue;
			}
			if(gamebi.getRGB(x+1,y+1)!=productcolor.getColor()[2][2]) {
				continue;
			}
			return productcolor;
		}
		return null;
	}
}
